package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.SupplierQueryVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;

    // 物流来源分页展示
    @Override
    public PageVO<Supplier> findSupplierList(SupplierQueryVO supplierQueryVO) {
        // 获取查询条件
        String address = supplierQueryVO.getAddress();
        String contact = supplierQueryVO.getContact();
        String name = supplierQueryVO.getName();
        // 从db中查询
        List<Supplier> suppliers = supplierMapper.querySupplier(address,contact,name);


        // 获取当前页和总记录数
        Integer pageSize = supplierQueryVO.getPageSize();
        Integer pageNum = supplierQueryVO.getPageNum();
        PageHelper.startPage(pageNum,pageSize);

        PageInfo pageInfo = new PageInfo(suppliers);
        PageVO<Supplier> pageVO = new PageVO<Supplier>(pageInfo.getTotal(),suppliers);
        return pageVO;
    }

    // 添加物流来源
    @Override
    public void add(Supplier supplier) {
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());
        supplierMapper.insert(supplier);
    }

    // 修改物流来源-根据id查询
    @Override
    public Supplier edit(Long id) {
        Supplier supplier = new Supplier();
        supplier.setId(id);
        return supplierMapper.selectByPrimaryKey(supplier);
    }

    // 修改物流来源
    @Override
    public void update(Supplier supplier) {
        supplierMapper.updateByPrimaryKey(supplier);
    }

    // 删除物流来源
    @Override
    public void delete(Long id) {
        Supplier supplier = new Supplier();
        supplier.setId(id);
        supplierMapper.delete(supplier);
    }

    // 获取所有来源数据-不分页
    @Override
    public List<Supplier> findAllSupplier() {
        return supplierMapper.selectAll();
    }
}
